﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Business;
using DataAccess;
using System.Web.Security;
public partial class admin_Create_Report : System.Web.UI.Page
{
	//Create new decryption object
	Decryption objdec = new Decryption();
	// String clientid = "1000";
	// int index = 0;
	protected void Page_Load(object sender, EventArgs e)
	{
		// CreateConn cc = new CreateConn();
		if (!IsPostBack)
		{
			//DataAccess.i2iPortalTableAdapters.ClientsTableAdapter CTAdapter = new DataAccess.i2iPortalTableAdapters.ClientsTableAdapter();
			//DataTable cdt = CTAdapter.GetData();
			//String clientName;
			string query_client = "select * from clients";
			CreateConn conobj2 = new CreateConn();
			DataTable cdt = conobj2.SQLConnDataTable(query_client);
			string clientName;
			//  Data.i2iPortal.ClientsDataTable
			//  Selectclient_DropDownList1.Items.Clear();

            foreach (DataRow client in cdt.Rows)
            {
                clientName = client["clientName"].ToString();
                var parentID = client["parent_client_id"];
                if (parentID != DBNull.Value && Int32.Parse(parentID.ToString()) != 0)
                {
                    continue;
                }else if(clientName.Equals("Administrator")){
                    continue;
                }
              
                String clientid = client["client_id"].ToString();
                ListItem clienitem = new ListItem(clientName, clientid, true);
                Selectclient_DropDownList1.Items.Add(clienitem);
            }

			int clientid2 = Int32.Parse(Selectclient_DropDownList1.SelectedValue);
			DataAccess.i2iPortalTableAdapters.SitesTableAdapter STA = new DataAccess.i2iPortalTableAdapters.SitesTableAdapter();
			string query_areas = "select * FROM clients where parent_client_id='" + clientid2 + "'";
			CreateConn conobj = new CreateConn();
			DataTable dt = conobj.SQLConnDataTable(query_areas);
			if (dt != null && dt.Rows.Count > 0)
			{
				foreach (DataRow dr in dt.Rows)
				{
					string area_name = dr["clientName"].ToString();
					string area_id = dr["client_id"].ToString();
					ListItem areitem = new ListItem(area_name, area_id, true);
					//areitem.Value = area_id;
					//areitem.Text = area_name;
					AreaDropDownList.Items.Add(areitem);



				}
			}
			else
			{
				AreaDropDownList.Items.Add(Selectclient_DropDownList1.SelectedItem);
				//DataTable st = STA.GetDataByClientID(clientid);
				//SitesCheckBoxList.Items.Clear();

				//foreach (DataRow site in st.Rows)
				//{
				//    String siteName = site["siteName"].ToString();
				//    string siteid = site["site_id"].ToString();

				//    CheckBox site_CB = new CheckBox();
				//    site_CB.Text = siteName;
				//    site_CB.ID = siteid;
				//    //SitesCheckBoxList.Items.
				//    SitesCheckBoxList.Items.Add(siteName);

				//}
			}

			//DataAccess.i2iPortalTableAdapters.SitesTableAdapter STA = new DataAccess.i2iPortalTableAdapters.SitesTableAdapter();
			System.Collections.IEnumerator sitesitems = AreaDropDownList.Items.GetEnumerator();
			if (sitesitems.MoveNext())
			{
				ListItem siteitem = (ListItem)sitesitems.Current;
				int sites_clientID = Int32.Parse((siteitem.Value));
				DataTable st = STA.GetDataByClientID(sites_clientID);
				foreach (DataRow site in st.Rows)
				{
					String siteName = site["siteName"].ToString();
					string siteid = site["site_id"].ToString();

					CheckBox site_CB = new CheckBox();
					site_CB.Text = siteName;
					site_CB.ID = siteid;
					//SitesCheckBoxList.Items.
					SitesCheckBoxList.Items.Add(siteName);
				}
			}
			// getSitesService.Service mysistes = new getSitesService.Service();
			//  AddCheckboxes("chansheng");
			//  Session["client_id"] = clientid;
			// SitesObjectDataSource.SelectParameters["client_id"].DefaultValue = clientid;
			// mysistes.getSites("4001");
		}
	}
	/// <summary>
	/// Handles the DataBound event of the FormViewReport control.
	/// Set the Component Name, if the page open from View_Component.aspx
	/// </summary>
	/// <param name="sender">The source of the event.</param>
	/// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
	protected void FormViewReport_DataBound(object sender, EventArgs e)
	{
	}
	/// <summary>
	/// Gets the comment decription
	/// from the database of the requested parameter
	/// </summary>
	/// <param name="comment_id">The comment_id.</param>
	/// <returns></returns>
	protected String GetComment(string comment_id)
	{
		CreateConn objcon = new CreateConn();
		string query = "SELECT commentDescription FROM StandardComments" +
			" WHERE comment_id = " + comment_id;
		DataTable dt = objcon.SQLConnDataTable(query);
		string comment = dt.Rows[0]["commentDescription"].ToString();

		return comment;
	}


	/// <summary>
	/// Handles the Click event of the InsertCancelButton control.
	/// Go to the source page
	/// </summary>
	/// <param name="sender">The source of the event.</param>
	/// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
	protected void InsertCancelButton_Click(object sender, EventArgs e)
	{
		string component_id = "";
		try
		{
			component_id = objdec.DecodeUrl(Request.QueryString["component_id"].ToString());
		}
		catch (Exception err)
		{
			Console.Write("Err msg:" + err.Message);
		}
		if (component_id != "")
		{
			Server.Transfer("View_Comp.aspx?component_id=" + component_id);
		}
		else
		{
			Server.Transfer("Mng_Report.aspx");
		}
	}
	/// <summary>
	/// Handles the ItemInserted event of the FormViewReport control.
	/// Go to the source page
	/// </summary>
	/// <param name="sender">The source of the event.</param>
	/// <param name="e">The <see cref="System.Web.UI.WebControls.FormViewInsertedEventArgs"/> instance containing the event data.</param>
	//protected void FormViewReport_ItemInserted(object sender, FormViewInsertedEventArgs e) {
	//    string component_id = "";
	//    TextBox dateRecorded = FormViewReport.FindControl("dateRecordedTextBox") as TextBox;
	//    try {
	//        component_id = objdec.DecodeUrl(Request.QueryString["component_id"].ToString());
	//    } catch (Exception err) {
	//        Console.Write("Err msg:" + err.Message);
	//    }
	//    if (component_id != "") {
	//        Server.Transfer("View_Comp.aspx?component_id=" + component_id);
	//    } else {
	//        //Server.Transfer("Mng_Report.aspx");
	//        Server.Transfer("View_Report.aspx?component_id=" + component_id + "&dateRecorded=" + dateRecorded.Text);
	//    }
	//}ClientsDropDownList
	protected void Client_changed(object sender, EventArgs e)
	{
		// Response.Redirect("lkj.aspx");
		SitesCheckBoxList.Items.Clear();
		AreaDropDownList.Items.Clear();
		int clientid = Int32.Parse(Selectclient_DropDownList1.SelectedValue);
		DataAccess.i2iPortalTableAdapters.SitesTableAdapter STA = new DataAccess.i2iPortalTableAdapters.SitesTableAdapter();
		string query_areas = "select * FROM clients where parent_client_id='" + clientid + "'";
		CreateConn conobj = new CreateConn();
		DataTable dt = conobj.SQLConnDataTable(query_areas);
		if (dt != null && dt.Rows.Count > 0)
		{
			AreaDropDownList.Items.Clear();
			foreach (DataRow dr in dt.Rows)
			{
				string area_name = dr["clientName"].ToString();
				string area_id = dr["client_id"].ToString();
				ListItem areitem = new ListItem(area_name, area_id, true);
				//areitem.Value = area_id;
				//areitem.Text = area_name;
				AreaDropDownList.Items.Add(areitem);



			}
		}
		else
		{
			AreaDropDownList.Items.Add(Selectclient_DropDownList1.SelectedItem);

		}
		System.Collections.IEnumerator sitesitems = AreaDropDownList.Items.GetEnumerator();
		if (sitesitems.MoveNext())
		{
			ListItem siteitem = (ListItem)sitesitems.Current;
			int sites_clientID = Int32.Parse((siteitem.Value));
			DataTable st = STA.GetDataByClientID(sites_clientID);
			foreach (DataRow site in st.Rows)
			{
				String siteName = site["siteName"].ToString();
				string siteid = site["site_id"].ToString();

				CheckBox site_CB = new CheckBox();
				site_CB.Text = siteName;
				site_CB.ID = siteid;
				//SitesCheckBoxList.Items.
				SitesCheckBoxList.Items.Add(siteName);
			}
		}
		// DropDownList clientsdp = (DropDownList)FormView1.FindControl("ClientsDropDownList");
		// //Console.Write("index value:"+clientsdp.SelectedValue);

		// clientid = clientsdp.SelectedValue;
		// SitesObjectDataSource.SelectParameters["client_id"].DefaultValue = clientid;

		//// clientsdp.DataBind();
		// //this.FormView1.DataBind();
		// CheckBoxList cbl = (CheckBoxList)FormView1.FindControl("SitesCheckBoxList");
		// CheckBox cb_all = (CheckBox)FormView1.FindControl("selectall");
		//cbl.DataBind();

		// cb_all.Visible = false;



		// cb_all.DataBind();
		//this.Page_Load(sender, e);

	}

	#region dyanamic checkboxes
	//Function to generate the dyanamic checkboxes
	private void AddCheckboxes(string strCheckboxText)
	{
		CheckBox chkList1;
		try
		{

			for (int intControlIndex = 0; intControlIndex < 5; intControlIndex++)
			{
				chkList1 = new CheckBox();
				chkList1.Text = strCheckboxText;
				chkList1.ID = "Chk" + intControlIndex;
				chkList1.Font.Name = "Verdana";
				chkList1.Font.Size = 9;
				chkList1.Visible = true;

			}
		}
		catch (Exception)
		{
			// throw new Exception(exp.Message);
		}
	}
	#endregion
	public void on_Button_submitClicked(object sender, EventArgs e)
	{
		Response.Redirect("Create_Report2.aspx");
	}

	protected void submit_button_Click(object sender, EventArgs e)
	{
		string clientid = Selectclient_DropDownList1.SelectedValue;
		string clientname = Selectclient_DropDownList1.SelectedItem.Text;
		Session["clientid"] = clientid;
		Session["clientname"] = clientname;
		string[] selectedsites = null;

		int i = 0;
		foreach (ListItem lt in SitesCheckBoxList.Items)
		{
			if (lt.Selected)
			{
				i = i + 1;//Get the count of selected sites
			}

		}
		int j = 0;
		selectedsites = new string[i];
		foreach (ListItem lt in SitesCheckBoxList.Items)
		{

			if (lt.Selected)
			{
				string value = lt.Text;
				Console.WriteLine(value);
				selectedsites[j] = value;
				j++;
			}

		}
		String date = DateTextBox.Text;
		int size = selectedsites.Length;
		Session["sitename"] = selectedsites;
		Session["SurveyDate"] = date;
		const bool oilreport = false;
		Session["OilReport"] = oilreport;
		Session["OldReport"] = CheckBox_OldReport.Checked;
		Response.Redirect("Main.aspx");
	}
	protected void AreaDropDownList_SelectedIndexChanged(object sender, EventArgs e)
	{

		int clientid = Int32.Parse(AreaDropDownList.SelectedValue);
		DataAccess.i2iPortalTableAdapters.SitesTableAdapter STA = new DataAccess.i2iPortalTableAdapters.SitesTableAdapter();

		DataTable st = STA.GetDataByClientID(clientid);
		SitesCheckBoxList.Items.Clear();

		foreach (DataRow site in st.Rows)
		{
			String siteName = site["siteName"].ToString();
			string siteid = site["site_id"].ToString();
			CheckBox site_CB = new CheckBox();
			site_CB.Text = siteName;
			site_CB.ID = siteid;
			//SitesCheckBoxList.Items.
			SitesCheckBoxList.Items.Add(siteName);

		}
	}
	protected void LoginStatus1_LoggingOut(object sender, LoginCancelEventArgs e)
	{


		Session.Contents.Remove("UserDetails");
		Session.Abandon();
		Session.Contents.RemoveAll();
		FormsAuthentication.SignOut();
		Response.Redirect("../Default.aspx");
	}
}
